/*
 * ConfirmPayWayPresenter.java
 * business
 *
 * Created by chentao on 2017/5/3.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.cashier;

import android.app.Activity;
import android.os.Bundle;

import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.ui.main.shoppingcart.multipayways.MultiPayWaysActivity;
import com.yidiandao.business.utils.ActivityStack;

import java.util.List;

/**
 * 收银台-Presenter
 */
public class CashierPresenter implements CashierContract.Presenter {

    private CashierContract.View mConfirmPayWayView;

    private String mOrderPayId;

    private String mUrl;

    private String mOnlinePayMoney;

    private boolean mAllOnlinePay;

    public CashierPresenter(CashierContract.View view, Bundle bundle) {
        mConfirmPayWayView = view;

        mOrderPayId = bundle.getString(Intents.EXTRA_ORDER_PAY_ID);
        mUrl = bundle.getString(Intents.HTTP_URL);

        mOnlinePayMoney = bundle.getString(Intents.EXTRA_ORDER_ONLINE_PAY_MONEY);

        mAllOnlinePay = bundle.getBoolean(Intents.EXTRA_ALL_ONLINE_PAY);

        mConfirmPayWayView.setPresenter(this);
    }

    @Override
    public void start() {
        mConfirmPayWayView.setTotalMoney(mOnlinePayMoney);
    }

    @Override
    public void kuaiQianPay() {
        finishMultiPayWaysActivity();
        mConfirmPayWayView.toKuaiQianPayActivity(mOrderPayId, mUrl);
    }

    @Override
    public void checkAllOnlinePay() {
        mConfirmPayWayView.finishActivity();

        if (mAllOnlinePay) {
            mConfirmPayWayView.toOrderResultActivity(mOrderPayId);
        }
    }

    // 点击了任意一个支付方式后，就不能返回多支付方式页面了
    private void finishMultiPayWaysActivity() {
        List<Activity> activityStack = ActivityStack.getActivityStack();
        for (int i = activityStack.size() - 1; i >= 0; i--) {
            Activity activity = activityStack.get(i);
            if (activity instanceof MultiPayWaysActivity) {
                activity.finish();
                break;
            }
        }
    }
}
